//
//  KRLoginViewController.m
//  KingrayTone_ios
//
//  Created by XiongYingqi on 14-5-14.
//  Copyright (c) 2014年 XiongYingqi. All rights reserved.
//

#import "KRLoginViewController.h"
#import "KRAppDelegate.h"

@interface KRLoginViewController ()

@end


@implementation KRLoginViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.userNameTextField.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
//    self.passwordTextField.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;



    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = YES;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];


//    self.scrollView.frame = CGRectMake(0, 0, 320, 460);
//    [self.scrollView setContentSize:CGSizeMake(320,651 )];

    [self configView];
}

- (void)configView {
//    UIButton *button = [[UIButton alloc] init];
//    self.loginButton =

//    UIColor *color = self.testButton.titleLabel.textColor;
//    CGFloat *cgFloat = CGColorGetComponents(color.CGColor);
//    NSLog(@"r: %f", cgFloat[0] * 255.0f);
//    NSLog(@"g: %f", cgFloat[1] * 255.0f);
//    NSLog(@"b: %f", cgFloat[2] * 255.0f);
//    NSLog(@"a: %f", cgFloat[3] * 255.0f);

    [self.loginButton setBackgroundImage:[[UIImage imageNamed:@"RegistrationNormal@2x.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:15] forState:UIControlStateDisabled];
    [self.loginButton setBackgroundImage:[[UIImage imageNamed:@"LoginGreenBigBtn"] stretchableImageWithLeftCapWidth:10 topCapHeight:15] forState:UIControlStateNormal];
}

- (void)keyboardHide:(UITapGestureRecognizer *)tap {
    [self.userNameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}


/**
* 点击return时隐藏键盘
*/
- (IBAction)hideKeyboard:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)didEnd:(id)sender {
    [self hideKeyboard:sender];
}

/**
* 点击空白时隐藏键盘
*/
- (IBAction)backViewTouch:(id)sender {
    NSLog(@"touch -------- ");
    // 发送resignFirstResponder.
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Private
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)setField:(UITextField *)field forKey:(NSString *)key {
    if (field.text != nil) {
        [[NSUserDefaults standardUserDefaults] setObject:field.text forKey:key];
    } else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
}

//键盘弹起后处理scrollView的高度使得textfield可见
- (void)keyboardDidShow:(NSNotification *)notification {
//    NSLog(@"show~~~~~%@", keyboardIsShown);
    if (keyboardIsShown) {
        return;
    }
    NSDictionary *info = [notification userInfo];
    NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= keyboardRect.size.height;
    self.view.frame = viewFrame;

    CGRect textFieldRect = [currentTextField frame];
//    [_scrollView scrollRectToVisible:textFieldRect animated:YES];

    NSLog(@"currentTextField~~~~~%@", currentTextField);
    keyboardIsShown = YES;
}

//键盘隐藏后处理scrollview的高度，使其还原为本来的高度
- (void)keyboardDidHide:(NSNotification *)notification {
    NSLog(@"hide~~~~~");
    NSDictionary *info = [notification userInfo];
    NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];


    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += keyboardRect.size.height;
    self.view.frame = viewFrame;


    keyboardIsShown = NO;
}

/**
* 重写
*/
- (void)textFieldDidBeginEditing:(UITextField *)textFieldView {
    currentTextField = textFieldView;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textFieldView {
    [textFieldView resignFirstResponder];
    return NO;
}

- (void)textFieldDidEndEditing:(UITextField *)textFieldView {
//    currentTextField = nil;
}


- (void)viewWillAppear:(BOOL)animated {
    self.userNameTextField.text = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];
    self.passwordTextField.text = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];

    //键盘弹起的通知, 使用UIKeyboardWillShowNotification更加平滑
    [[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(keyboardDidShow:)
                   name:UIKeyboardWillShowNotification
                 object:self.view.window];

    //键盘隐藏的通知
    [[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(keyboardDidHide:)
                   name:UIKeyboardWillHideNotification
                 object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter]
            removeObserver:self
                      name:UIKeyboardDidShowNotification
                    object:nil];

    [[NSNotificationCenter defaultCenter]
            removeObserver:self
                      name:UIKeyboardDidHideNotification
                    object:nil];
}


- (IBAction)doLogin:(id)sender {
    NSLog(@"connect ---- ");
    NSLog(self.userNameTextField.text);
    NSLog(self.passwordTextField.text);
    [self setField:self.userNameTextField forKey:kXMPPmyJID];
    [self setField:self.passwordTextField forKey:kXMPPmyPassword];
    NSLog(@"connect2 ---- ");


    KRAppDelegate *appDelegate = [KRAppDelegate appDelegate];

    bool connected = [appDelegate connect];
    if (connected) {

        NSLog(@"connected ---- ");
    }

}

@end
